package com.xci.edu.module.foreground.pc.train.controller;

import com.github.lvyanyang.core.BoolResult;
import com.xci.edu.core.entity.ExerciseQuestion;
import com.xci.edu.core.service.EduQuestionFavoriteService;
import com.xci.edu.core.service.EduQuestionService;
import com.xci.edu.core.service.TrainStudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/train/simulatorExam")
public class SimulatorExamController {
    /** 试题库服务 */
    @Resource
    private EduQuestionService eduQuestionService;
    /** 学员培训登录 */
    @Resource
    private TrainStudentService trainStudentService;
    /** 题库收藏服务 */
    @Resource private EduQuestionFavoriteService eduQuestionFavoriteService;

    /** 试题集 */
    private List<ExerciseQuestion> questionList;
    /** 学员Id*/
    private Long studentId;

    /** 考试状态 true-提交 false-未提交 */
    private Boolean submitStatus;

    /** 模拟考试页面 */
    @GetMapping("/index")
    public String index() {
        //根据用户科目id查询试题，答题状态设为未提交
        Long id = trainStudentService.getCurrentStudent().getEduCategoryId();
        questionList = eduQuestionService.getExerciseQuestions(id,null);
        submitStatus =false;
        studentId = trainStudentService.getCurrentStudent().getId();
        return "foreground/pc/train/eduSimulatorExam/index";
    }

    /** 获取考试题 */
    @PostMapping("/getQuestion")
    public String getQuestion(@RequestParam(defaultValue = "0" ) Integer index, ModelMap map){
        ExerciseQuestion entity = null;
        if (index >= 0 && index < questionList.size()) entity = questionList.get(index);
        map.put("entity", entity);
        map.put("index", index);
        map.put("size", questionList.size());
        map.put("isCommit",submitStatus);
        map.put("questList",questionList);
        return "foreground/pc/train/eduSimulatorExam/exam";
    }

    /** 记录答题情况 */
    @ResponseBody
    @PostMapping("/checkQuestion")
    public BoolResult checkQuestion(Long questionId, Boolean userResult, String userAnswer){
        questionList.stream().filter(
                item->item.getId().equals(questionId)
        ).findFirst().ifPresent(q->{
            q.setUserSelected(true);
            q.setUserResult(userResult);
            q.setUserAnswer(userAnswer);
        });
        if (!userResult){
            eduQuestionFavoriteService.insertError(studentId, questionId);
        }
        return BoolResult.success();
    }

    /** 交卷 */
    @ResponseBody
    @PostMapping("/commit")
    public BoolResult commit(){
        //设置提交状态
        submitStatus =true;
        //总分,总题数,答对题数,答错数,用户分数
        int totalScore= 0,totalCount= 0,correctCount= 0,errorCount= 0,userScore= 0 ;
        for(ExerciseQuestion quest:questionList ){
            totalCount++;
            totalScore += quest.getScore();
            if (quest.getUserResult()){
                userScore+= quest.getScore();
                correctCount++;
            }else{
                errorCount++;
            }
        }
        //设置返回参数
        Map msg = new HashMap();
        msg.put("totalScore",totalScore);
        msg.put("totalCount",totalCount);
        msg.put("correctCount",correctCount);
        msg.put("errorCount",errorCount);
        msg.put("userScore",userScore);
        return BoolResult.success(msg);
    }
}
